﻿using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.ServiceContracts;
using KendoGridBinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkTicket.Application.Implementation;
using WorkTicket.Website.Common;

namespace WorkTicket.Website.Controllers
{
    public class SizeGroupController : Controller
    {
        protected ISizeGroupService sizeGroupService = new SizeGroupServiceImpl();
        protected ISizeService sizeService = new SizeServiceImpl();
        //
        // GET: /SizeGroup/

        public ActionResult Index()
        {
            return View();
        }


        public JsonResult GetAllItems(KendoGridRequest request)
        {
            List<SizeGroup> result = sizeGroupService.GetAll();
            return Json(new { data = result, total = result.Count },JsonRequestBehavior.AllowGet); 
        }

        //
        // GET: /SizeGroup/Create
        [HttpGet]
        public ActionResult Create()
        {
            SizeGroup sizeGroup = new SizeGroup();
            sizeGroup.Sizes = new List<Size>();
            ViewData["SelectedSizeList"] = from a in sizeGroup.Sizes
                                            select new SelectListItem
                                            {
                                                Text = a.Name,
                                                Value = a.SizeID.ToString()
                                            };
            this.GetSizeList(sizeGroup);
            ViewData["HideValue"] = string.Empty;
            return View(sizeGroup);
        }

        //
        // POST: /SizeGroup/Create

        [HttpPost]
        public ActionResult CreateAndEdit(SizeGroup model)
        {
            string strTemp = Request["hdSelectedSize"];
            string[] strList = strTemp.Split(';');

            model.Sizes = new List<Size>();
            model.SizeGroupSize = new SizeGroupSize();
            foreach (var item in strList)
            {
                if (item != "")
                {
                    Size size = new Size();
                    size.SizeID = Guid.Parse(item.Split(',')[0]);
                    size.Name = item.Split(',')[1];
                    model.Sizes.Add(size);
                }
            }
            
            try
            {
                // TODO: Add insert logic here
                if (Request.IsAjaxRequest())
                {
                    if (model.SizeGroupID.ToString() == "00000000-0000-0000-0000-000000000000") //add new entity
                    {
                        model.SizeGroupID = Guid.NewGuid();
                        if (ModelState.IsValid)
                        {
                            sizeGroupService.Add(model);
                        }
                        else
                        {
                            return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
                        }
                    }
                    else  //update entity
                    {
                        if (ModelState.IsValid)
                        {
                            sizeGroupService.Update(model);
                        }
                    }
                    
                }
                return Json(new { data = true, message = "OK" }, JsonRequestBehavior.AllowGet); 
            }
            catch
            {
                return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
            }
        }

        //
        // GET: /SizeGroup/Edit/5
        [HttpGet]
        public ActionResult Edit(string id)
        {
            var sizeGroup= sizeGroupService.GetItemByID(id);
            ViewData["SelectedSizeList"] = from a in sizeGroup.Sizes
                                    select new SelectListItem
                                    {
                                        Text = a.Name,
                                        Value = a.SizeID.ToString()
                                    };
            string strTemp = string.Empty;
            foreach (var item in sizeGroup.Sizes)
            {
                strTemp = strTemp + item.SizeID.ToString() + "," + item.Name + ";";
            }
            ViewData["HideValue"] = strTemp;
            this.GetSizeList(sizeGroup);
            return View("Create",sizeGroup);
        }

        //
        // GET: /SizeGroup/Delete/5o
        public ActionResult Delete(string id)
        {
            try
            {
                sizeGroupService.Delete(id);
                return Json(new { message = "ok" },JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { message = "no" }, JsonRequestBehavior.AllowGet);
            }
        }


        [HttpPost]
        public JsonResult CheckNameExisted()
        {
            string name = Request["Name"];
            string id = Request["ID"];
            JsonResult result = new JsonResult();
            result.Data = sizeGroupService.CheckNameExisted(name, Guid.Parse(id));
            return result;
        }

        private void GetSizeList(SizeGroup sizeGroup)
        {
            List<SelectListItem> sourceList = new List<SelectListItem>();
            foreach (var item in sizeService.GetAll())
            {
                if (!sizeGroup.Sizes.Exists(c => c.SizeID == item.SizeID))
                {
                    sourceList.Add(new SelectListItem { Text = item.Name, Value = item.SizeID.ToString() });
                }
            }
            ViewData["SizeList"] = sourceList.AsEnumerable();

        }


    }
}
